define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var TYPE = pubjs.config("CONST/OFFLINE_TYPE");
	var TIME_TYPE = {
		DAY: 0, WEEK: 1, MONTH: 2
	};
	var Base = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-reportEditType',
				vModel: {
					'reportType': TYPE.COMMON,
					'emails':'',
					'TYPE': TYPE,
					'TIME_TYPE': TIME_TYPE,
					'timeType': 0,
					'isEdit': false,
				}
			});
			this.Super('init', arguments);
		},
		afterBuild: function(){
			this.append(`
				<div >
					<div class="M-formSectionItem ">
						<label>
							<input type="radio" v-model="reportType" v-bind:disabled="isEdit"  v-bind:value="TYPE.COMMON"/>
							<span>${LANG('普通报表')}</span>
						</label>
						<label class="mr10">
							<input type="radio" v-model="reportType"  v-bind:disabled="isEdit" v-bind:value="TYPE.SCHEDULE"/>
							<span>${LANG('定时报表')}</span>
						</label>
						<span pub-name="tipReport" pub-mod="@base/tip.desc"/>
					</div>
					<div class="M-formConGroup" v-show="reportType == TYPE.COMMON">
						<label class="group-label timeLabel">${LANG('时间选择：')}</label>
						<div class="group-content" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>
					</div>
					<div class="M-formConGroup" v-show="reportType == TYPE.SCHEDULE">
						<label  class="group-label timeLabel mr10">${LANG('时间周期设置：')}</label>
						<div  pub-name="datePeriod" pub-mod="modules/labelSelect.base"/>
						<span pub-name="tipPeriod" pub-mod="@base/tip.desc"/>
						<div class="M-formConGroup ml40 mt30" v-show="timeType == TIME_TYPE.WEEK">
							<label  class="group-label timeLabel">${LANG('节点选择：')}</label>
							<div class="group-content" pub-name="dateSelectWeek" pub-mod="@base/common/drop.drop"/>
						</div>
						<div class="M-formConGroup ml40 mt30" v-show="timeType== TIME_TYPE.MONTH">
							<label  class="group-label timeLabel">${LANG('节点选择：')}</label>
							<div class="group-content" pub-name="dateSelectMouth" pub-mod="@base/common/drop.drop"/>
						</div>
						<div class="M-formConGroup" >
							<label  class="group-label timeLabel">${LANG('邮箱设置：')}</label>
							<div  class="group-content">
								<textarea class="h200 w60pct mr10" v-model="emails"></textarea>
								<span pub-name="tips" pub-mod="@base/tip.desc"/>
							</div>
						</div>
					</div>
				</div>
			`);
			this.vm.mount();
			this.buildFromTemplate({
				tips: {
					'data': LANG('默认为账号注册邮箱，可分行填写多个。报表以附件的形式发送到填写的邮箱')
				},
				tipPeriod: {
					'data':LANG('<li>选择日周期，每天的0点自动跑昨天00：00到23：59的数据。</li> ' +
						'<li>选择周周期，每周的节点日跑出上一个自然周的数据，如,节点选择为星期三，则每周三自动跑出上周三到这周二的数据</li> ' +
						'<li>选择月周期，根据选择的月节点，每月的节点日跑出上一个自然月的数据，如,节点选择为5日，则每月的5日自动跑出上个月5日到这个月4日的数据</li>')
				},
				tipReport: {
					'data': LANG('以指定的时间产生报表。')
				},
				date: {
					'showButton': false
				},
				datePeriod: {
					singleSelect: true,
					changeFire: true,
					data: [
						{id: TIME_TYPE.DAY, name: LANG('日')},
						{id: TIME_TYPE.WEEK, name: LANG('周')},
						{id: TIME_TYPE.MONTH, name: LANG('月')},
					]
				},
				dateSelectWeek:{
					'options':  [
						{id: 1, name: LANG('星期一')},
						{id: 2, name: LANG('星期二')},
						{id: 3, name: LANG('星期三')},
						{id: 4, name: LANG('星期四')},
						{id: 5, name: LANG('星期五')},
						{id: 6, name: LANG('星期六')},
						{id: 0, name: LANG('星期日')},
					]
					,"value": 1
				},
				dateSelectMouth:{
					panel_config:{
						height:300
					},
					'options': this.getMouthDay()
					,"value": 1
					,disabled:true
				}
			});
		},
		getMouthDay: function () {
			var MouthDay =[];
			MouthDay.push({id: 293031, name: LANG("最后一日")});
			for(var i =1 ; i<=28; i++){
				MouthDay.push({id: i, name: LANG(i+'日')})
			}
			return MouthDay;
		},
		// 一些getData，setData之类的处理还没弄~
		onDataChange: function (ev) {
			if(ev.name == "datePeriod") {
				this.vm.$.timeType = ev.param;
			}
		},
		getData: function () {
			var ret = {};
			var data= {};
			var vm = this.vm.$;
			ret.Type = +vm.reportType;
			if(ret.Type == TYPE.COMMON) {
				ret.Param = this.$['date'].getData(true);
			}
			else {
				switch (vm.timeType){
					case TIME_TYPE.DAY:
						break;
					case TIME_TYPE.WEEK:
						data = this.$.dateSelectWeek.getValue();
						break;
					case TIME_TYPE.MONTH:
						data = this.$.dateSelectMouth.getValue();
						break;
				}

				data.Type = vm.timeType; //0日1周2月
				data.Email = vm.emails.replace(/[\r\n]+/g, '\n').split('\n');
				ret.Param = data;
			}

			return ret;
		},
		setData: function(data){
			var vm = this.vm.$;
			var c = this.getConfig();
			vm.reportType = data.Type; //普通报表or实时报表
			if(data.hasOwnProperty('isEdit')){
				vm.isEdit = data.isEdit;
			}
			if(data.Type == TYPE.COMMON){ //2
				this.$['date'].setData(data.Param);
			}
			else
			{
				vm.timeType = data.Param.Type; //0日1周2月
				this.$.datePeriod.setValue(data.Param.Type).disable();
				switch (vm.timeType){
					case TIME_TYPE.WEEK:
						this.$.dateSelectWeek.setValue(data.Param.id).disable();
						break;
					case TIME_TYPE.MONTH:
						this.$.dateSelectMouth.setValue(data.Param.id).disable();
						break;
				}

				vm.emails = data.Param.Email.join("\n") ;
			}

			return this;
		}
		,reset:function () {
			this.vm.reset();
			this.$.datePeriod.enable();
			this.$.dateSelectWeek.enable();
			this.$.dateSelectMouth.enable();
		}
	});
	exports.base = Base;
});